package com.yanqu.road.dao.impl.activity.mammonhall;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.mammonhall.MammonHallSourceDataInfo;
import com.yanqu.road.entity.activity.mammonhall.MammonHallSourceKV;
import com.yanqu.road.entity.activity.mammonhall.MammonHallTimeInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MammonHallConfigDaoImpl extends TempDao {

    private static String getMammonHallTimeInfoSql = "select * from `t_s_activity_mammon_hall_time` where `activityId`=? ORDER BY `startTime` ASC";
    public List<MammonHallTimeInfo> getMammonHallTimeInfoList(int activityId) {
        List<MammonHallTimeInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getMammonHallTimeInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                MammonHallTimeInfo data = new MammonHallTimeInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setType(rs.getInt("type"));
                data.setStartTime(rs.getInt("startTime"));
                data.setEndTime(rs.getInt("endTime"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getMammonHallSourceDataInfoSql = "select * from `t_c_activity_mammon_hall_source`  ORDER BY `rank` ASC";
    public Map<Integer, List<MammonHallSourceDataInfo>> getMammonHallSourceDataInfoList() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<MammonHallSourceDataInfo>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getMammonHallSourceDataInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                MammonHallSourceDataInfo data = new MammonHallSourceDataInfo();
                data.setUserId(rs.getLong("userId"));
                data.setServerId(rs.getLong("serverId"));
                data.setType(rs.getInt("type"));
                data.setActivityId(rs.getInt("activityId"));
                data.setDataTime(rs.getInt("dataTime"));
                data.setRank(rs.getInt("rank"));
                data.setValue(rs.getString("value"));
                String strValue = rs.getString("userBaseInfo");
                if(!StringUtils.isNullOrEmpty(strValue)) {
                    data.setUserBaseInfo(JSON.parseObject(strValue, new TypeReference<UserBaseInfo>() {}));
                }
                String otherInt = rs.getString("otherInt");
                if(!StringUtils.isNullOrEmpty(otherInt)){
                    data.setOtherInt(StringUtils.stringToIntegerList(rs.getString("otherInt"), ","));
                }
                data.setOtherString(StringUtils.stringToStringList(rs.getString("otherString"), "@#"));
                List<String> strArr = StringUtils.stringToStringList(rs.getString("kvList"), "\\|");
                for (String str : strArr) {
                    List<Long> valArr = StringUtils.stringToLongList(str, ",");
                    MammonHallSourceKV kv = new MammonHallSourceKV();
                    kv.setItem(valArr.get(0).intValue());
                    kv.setCount(valArr.get(1));
                    data.getKvList().add(kv);
                }
                if(!result.containsKey(data.getType())){
                    result.put(data.getType(), new ArrayList<>());
                }
                result.get(data.getType()).add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

}
